package com.yidu.hbatsft.action;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.hbatsft.domain.*;
import com.yidu.hbatsft.service.*;
import com.yidu.privilege.Utils.BaseMessage;
import com.yidu.utils.FileTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/atsCurriclum")
public class AtsPlanCurriculumController {
    @Resource
    AtsPlanCurriculumService atsPlanCurriculumService;//阶段计划

    @Resource
    AtsBaseSportService atsBaseSportService;// 体育项目管理

    @Resource
    AtsEmpCoachService atsEmpCoachService;// 教练员

    @Resource
    AtsPlanItemService atsPlanItemService;//指标

    @Resource
    AtsPlanWeekService atsPlanWeekService;//周

    @Resource
    FileTools fileTools;//文件上传

    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String,Object> selectAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, AtsPlandimSearch atsPlandimSearch){
        Map<String, Object> map = new HashMap<String, Object>();
        List<AtsPlanCurriculum> list = atsPlanCurriculumService.selectAll(pageNum,pageSize,atsPlandimSearch);//分页数据
        Integer count = atsPlanCurriculumService.count(atsPlandimSearch);

        map.put("code", 0);
        map.put("msg", "");
        map.put("msg", "");
        map.put("count", count);
        map.put("data",list);

        return map;
    }

    @RequestMapping("selMessage")
    @ResponseBody
    public Map<String,Object> selMessage(){

        Map<String, Object> map = new HashMap<String, Object>();
        //所有运动项目名
        List<AtsBaseSport> atsBaseSport = atsBaseSportService.selAllSportNmae();
        //所有教练员的姓名
        List<AtsEmpCoach> atsEmpCoach = atsEmpCoachService.selAllCoachName();
        //指标
        List<AtsPlanItem> atsPlanItem = atsPlanItemService.selectItem("5");
        //所有周计划名称
        List<AtsPlanWeek> atsPlanWeek =  atsPlanWeekService.selectWeekName();

        map.put("sport",atsBaseSport);
        map.put("coach",atsEmpCoach);
        map.put("item",atsPlanItem);
        map.put("week",atsPlanWeek);

//        ObjectMapper mapper = new ObjectMapper();
//        String rs = null;
//        try {
//            rs = mapper.writeValueAsString(map);
//            System.out.println(rs);
//        } catch (Exception e) {
//            System.out.println(123);
//        }

        return map;
    }

    @RequestMapping("addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(AtsPlanCurriculum atsPlanCurriculum, AtsPlanItem atsPlanItem , String itemIdArry) throws ParseException {
        BaseMessage bm = new BaseMessage();

        atsPlanCurriculum.setCreatTime(new Date());//设置创建时间
        atsPlanCurriculum.setCreater("sb");//设置当前登陆人

        atsPlanItemService.itemUpdte("5");//将上一次指标都改为0

        AtsBaseSport atsBaseSport = atsBaseSportService.selSportNmaeLX(atsPlanCurriculum.getSportId());//查询出atsPlanYear的item名
        atsPlanCurriculum.setItemName(atsBaseSport.getSportName());

        Integer temp = null;
        System.out.println("id:"+atsPlanCurriculum.getCurriculumId());
        if(atsPlanCurriculum.getCurriculumId() != 0){
            temp = atsPlanCurriculumService.updateByPrimaryKeySelective(atsPlanCurriculum,atsPlanItem,itemIdArry);
        }else{
            temp = atsPlanCurriculumService.insertSelective(atsPlanCurriculum,atsPlanItem);
        }
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("操作成功");
        }else {
            bm.setState(0);
            bm.setMsg("操作失败");
        }
        return bm;
    }

    @RequestMapping("updSel")
    @ResponseBody
    public Map<String,Object> selectByPrimaryKey(Integer curriculumId){
        AtsPlanCurriculum atsPlanCurriculum = null;
        List<AtsPlanItem> atsPlanItem = null;
        Map<String, Object> map = null;
        if (curriculumId != null && !"".equals(curriculumId)){
            map = new HashMap<String, Object>();
            atsPlanCurriculum = atsPlanCurriculumService.selectByPrimaryKey(curriculumId);
            atsPlanItem = atsPlanItemService.selectItemByPid(curriculumId.toString(),"5");

            List<String> list = null;
            if(atsPlanCurriculum.getOtherCoach() != null && !"".equals(atsPlanCurriculum.getOtherCoach())) {
                String otherNameTemp[] = atsPlanCurriculum.getOtherCoach().split(",");
                list = new ArrayList<String>();
                if (otherNameTemp[0] != null && !"".equals(otherNameTemp[0])) {
                    for (int i = 0; i < otherNameTemp.length; i++) {
                        String otherName = otherNameTemp[i];
                        AtsEmpCoach atsEmpCoach = atsEmpCoachService.selCoachId(otherName);
                        list.add(atsEmpCoach.getCoachId().toString());
                    }
                } else {
                    list.add(null);
                }
            }
            map.put("YearMore",atsPlanCurriculum);
            map.put("Item",atsPlanItem);
            map.put("otherNameId",list);
        }
        return map;
    }

    @RequestMapping("Delete")
    @ResponseBody
    public BaseMessage updateDelete(String ids){
        BaseMessage bm = new BaseMessage();
        Integer temp = atsPlanCurriculumService.updateDelete(ids);
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("删除成功");
        }else {
            bm.setState(0);
            bm.setMsg("删除失败");
        }
        return bm;
    }


}
